package components
{
	import flash.utils.Dictionary;
	
	public final class Categories
	{
		// **************** Private Variables ********************* //
		private var _categories:Dictionary = new Dictionary();
		// **************** END Private Variables ********************* //
		
		// ********************** Accessors ********************* //
		public function get categories():Dictionary
		{
			return _categories;
		}
		
		public function set categories(value:Dictionary):void
		{
			_categories = value;
			instance.categories = value;
		}
		// ********************** END Accessors ********************* //
		
		// **************** Singleton Code **************** //
		private static var instance:Categories = new Categories();

		public function Categories() 
		{
			if( instance ) 
				throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
		}
		public static function getInstance():Categories 
		{
			return instance;
		}
		// **************** END Singleton Code **************** //
		
		public function addCategory(name:String, details:Object):void
		{
			var catColor:uint;
			if(categories.hasOwnProperty(name))
				catColor = categories[name].color;
			else
				catColor = getNewColor();
			details.color = catColor;
			categories[name] = details;
		}
		
		// ***************** Color Utility Functions ******************** //
		private function getNewColor():uint
		{
			var newColor:uint = Math.random() * uint.MAX_VALUE;
			while(!isNewColor(newColor))
				newColor = Math.random() * uint.MAX_VALUE;
			return newColor;
		}
		
		private function isNewColor(newColor:uint):Boolean
		{
			for each(var cat:Object in categories)
				if(cat.color == newColor)
					return false;
			return true;
		}
		// ***************** END Color Utility Functions ******************** //
	}
}